package com.shujing.controller;

import com.shujing.dto.PasswordDTO;
import com.shujing.pojo.Result;
import com.shujing.pojo.User;
import com.shujing.service.UserService;
import com.shujing.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 陈书憬
 * @version 1.0
 * 2024/4/7
 */
@Slf4j
@RestController
@Validated
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * @param username
     * @param password
     * @return 该接口用于注册新用户
     */
    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}") String username, @Pattern(regexp = "^\\S{5,16}") String password) {
        log.info("注册操作...传入的用户名和密码为：{},{}", username, password);
        User user = userService.findByName(username, password);
        if (user != null) {
            return Result.error("该用户名已经存在！");
        } else {
            return Result.success();
        }
    }

    /**
     * 该接口用于登录
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Result login(@Pattern(regexp = "^\\S{5,16}") String username, @Pattern(regexp = "^\\S{5,32}") String password) {
        log.info("登录操作...传入的用户名和密码为：{},{}", username, password);
        String user = userService.login(username, password);
        return Result.success(user);
    }

    /**
     * 获取用户详细信息
     *
     * @return
     */
    @GetMapping("/userInfo")
    public Result<User> userInfo(/*@RequestHeader(name = "Authorization") String token*/) {
//        Map<String, Object> map = JwtUtil.parseToken(token);
//        String username = (String) map.get("username");

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");

        User user = userService.userInfo(username);
        return Result.success(user);
    }

    /**
     * 更新用户基本信息
     *
     * @param user
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody @Validated User user) {
        log.info("更新前的数据：{}", user);
        userService.update(user);
        return Result.success();
    }

    /**
     * 更新用户头像
     *
     * @param avatarUrl
     * @return
     */
    @PatchMapping("updateAvatar")
    public Result updateAvatar(@RequestParam @URL String avatarUrl) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");

        User user = new User();
        user.setId(userId);
        user.setUserPic(avatarUrl);

        userService.update(user);
        return Result.success();
    }

    /**
     * 更新用户密码
     *
     * @param passwordDTO
     * @return
     */
    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody PasswordDTO passwordDTO, @RequestHeader("Authorization") String token) {
        log.info("传入的密码参数为：{}", passwordDTO);
        userService.updatePassword(passwordDTO);

        // 删除redis中对应的token
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.getOperations().delete(token);

        return Result.success();
    }
}
